package com.vhall.component.service.room.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.service.upload.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 生成二维码公共类
 *
 * @author shimingming
 * @since 2023/8/10 16:57
 */
@Slf4j
@Component
public class QrCodeUtils {

    @Autowired
    private UploadService uploadService;


    public String generateQRCodeByUrl(String url) {
        try {
            BufferedImage qrCodeImage = generateQRCode(url, 300, 300);
            return saveQRCodeImage(qrCodeImage, "output.png");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("生成二维码异常！" + e.getMessage());
        }
    }


    /**
     * 生成二维码图像
     */
    public BufferedImage generateQRCode(String link, int width, int height) throws WriterException {
        Map<EncodeHintType, Object> hintMap = new HashMap<>();
        // 设置二维码的纠错级别
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        // 设置字符编码
        hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(link, BarcodeFormat.QR_CODE, width, height, hintMap);

        int matrixWidth = bitMatrix.getWidth();
        BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();

        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, matrixWidth, matrixWidth);
        graphics.setColor(Color.BLACK);

        for (int i = 0; i < matrixWidth; i++) {
            for (int j = 0; j < matrixWidth; j++) {
                if (bitMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }

        return image;
    }

    /**
     * 保存二维码图像到文件
     */
    public String saveQRCodeImage(BufferedImage image, String filePath) throws IOException {
        // 图像格式
        String format = "png";
        File file = new File(filePath);
        ImageIO.write(image, format, file);

        try {
            MultipartFile multipartFile = new MockMultipartFile(file.getName(), "output.png", "png", new FileInputStream(file));
            Map<String, String> result = uploadService.doUpload(multipartFile, "");
            return result.get("url");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }

    }


}